﻿using System;
using System.Windows.Input;

namespace MyWindowsMediaPlayer.Libs.ViewModel
{
	public class RelayCommand : ICommand
	{
		private readonly Action execute;
		private readonly Func<bool> canExecute;

		public event EventHandler CanExecuteChanged
		{
			add
			{
				if (this.canExecute != null)
					CommandManager.RequerySuggested += value;
			}
			remove
			{
				if (this.canExecute != null)
					CommandManager.RequerySuggested -= value;
			}
		}

		public RelayCommand ( Action execute )
			: this(execute, null)
		{
		}

		public RelayCommand ( Action execute, Func<bool> canExecute )
		{
			if (execute == null)
				throw new ArgumentNullException("execute");
			this.execute = execute;
			this.canExecute = canExecute;
		}

		public void Execute ( object parameters )
		{
			this.execute();
		}

		public bool CanExecute ( object parameters )
		{
			return this.canExecute == null ? true : this.canExecute();
		}
	}
}
